<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;

class AuditingController extends BaseController {
   public function index(){
			$Mdo = D("Admin/User");
			$ulist = null;
			$wheres = array();
			$user = I('get.user', '', 'trim');
			$number = I('get.number', false, 'intval');
			$nickname= I('get.nickname', '', 'trim');
			$sector = I('get.sector', -1, 'intval');
		    $status=I('get.status',false,'intval');
			$psize = I('get.psize', $this->$m_psize,'intval');
		if (!$psize)
			$psize = $this->m_psize;
		else if ( $psize > 20 )
			$psize = $this->m_psize;
		/*if (-1 != $sector)
				$wheres['sector.sector_id'] = array('eq' , $sector);*/
		if(-1 != $sector) {
			if($sector != 1){
				$sidArr = array($sector);
				$ziId = M('sector')->where('parent_sector='.$sector)->field('sector_id')->select();
				// dump($ziId);
				if($ziId){
					foreach ($ziId as  $value) {
						foreach ($value as $key => $val) {
							array_push($sidArr,$value[$key]);
						}
					}	
				}
			$wheres['user.sector_id'] = array('in' , $sidArr);
			}
		}
		if (-1 == $status)
				 $wheres['user_extension.ext_sh_status']  =array('eq',1);
	    if($number ){
	    	$wheres['user_extension.ext_yggh'] = array('like' , '%'.$number.'%');
	    }
		if($nickname)
			$wheres['user_extension.ext_name'] = array('like', '%'.$nickname.'%');	

		$cnt = $Mdo
				->join('LEFT JOIN sector on sector.sector_id=user.sector_id')
				->join('LEFT JOIN user_extension on user.user_id= user_extension.user_id')
				->where( $wheres )->count();
		$Page = new \Common\ORG\Page( $cnt, $psize );
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$ulist = $Mdo->field("user.user_id,user.login_name,user.nickname,user.sector_id,user.role_id,user.phone,user.qq,user.weixin,user.email,user.state,user.last_time,sector.sector_name as sname ,user_extension.ext_yggh,user_extension.ext_sh_status,user_extension.ext_name")
		               ->where( $wheres )
					   ->order('user_id desc')
			           ->join('LEFT JOIN sector on sector.sector_id=user.sector_id')
					   ->join('LEFT JOIN user_extension on user.user_id= user_extension.user_id')
					   ->limit($Page->firstRow.','.$Page->listRows )
					   ->select();

		if( $ulist ){
			$res['rcode'] = 1;
			$res['data'] = $ulist;
			$res['pall'] =  $Page->getTotalPages();
			$res['p'] = $Page->nowPage;
			$res['psize'] = $psize;
		}else{
			$res['rcode'] = 2;
			$res['msg'] = "用户记录为空";
		}
		$res['number'] = $number;
		e_json(	$res);
	 }

	public function getPageParam(){
		$res = array('rcode'=>1,'sector'=>array(), 'role'=>array(),'msg'=>'');
		$wheres = array();
		$wheres['is_deleted'] = array('neq', 1);
		$MRole = D('Admin/Role');
		$roles = $MRole->field("role_id,role_name,parent_role")->where( $wheres )->select();
		$_roles = array();
        foreach( $roles as $vs ){
            $_roles[$vs['role_id']] = $vs;
        }
        if( 1 == $this->m_user['user_id'])
	       $arr = list_to_tree($roles,'role_id','parent_role', '_child', 0);
       else{
       		$arr = array();
       		$arr[] = $_roles[$this->m_user['role_id']];
		    $arr[0]['_child'] =  list_to_tree($roles,'role_id','parent_role', '_child', $this->m_user['role_id']);
       }
	   $res['role'] = $arr;
	   $MSector = D('Admin/Sector');
	   $secs = $MSector->field("sector_id,info,sector_name,parent_company,parent_sector,manager_id")->where( $wheres )->select();
       $_secs = array();
	   $arr = array();
    	foreach ($secs as $sc ){
            $_secs[$sc['sector_id']] = $sc;
        }
        if (1 == $this->m_user['user_id']) {
            $arr = list_to_tree($secs,'sector_id','parent_sector','_child', 0 );
        }
        else {
            $arr = array();
            $arr[]=$_secs[$this->m_user['sector_id']];
		    $arr[0]['_child'] = list_to_tree($secs,'sector_id','parent_sector','_child', $this->m_user['sector_id']);
        }
	   $res['sector'] = $arr;
		e_json( $res );
	}
	public function update(){
      $res = array('rcode'=>0,'msg'=>'服务器忙，请稍候再试');
      $userid=I('get.user_id');
      $where['user_id'] = $userid;
      $addData = M("User")->where($where)->find();
      $param = I('param');
      if($param == 1){
      	$updateData['ext_sh_status'] = 2;
      }else{
      	$updateData['ext_sh_status'] = 3;
      	
      }
		  $addres = M("UserExtension")->where($where)->save($updateData);
        
	   if( $addres){
	   	if($param==1){
	   		import("Common.ORG.Sentmsg");
           $msg='你的员工入职资料已经审核通过';
		   $empSent = new \Common\ORG\Sentmsg();
		    $resMsg = $empSent->sent($addData['phone'],$msg);
            $res['parames'] =$param;
	   	    $res['rcode'] = 1;
			$res['msg']  ='提交成功';
	   	}else if($param==2){
	   		$msg='你的员工入职资料信息不完整, 已经被退回修改,请尽快完善资料';
		    $empSent = new \Common\ORG\Sentmsg();
		    $resMsg = $empSent->sent($addData['phone'],$msg);
            $res['parames'] =$param;
	   	    $res['rcode'] = 1;
			$res['msg']  ='提交成功';
	   	}
	   		
	    }else{
	   	   $res['msg']='提交失败';
	   	};
	   e_json($res);
	}
}